﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.DAL
{
    public class Menu : Base<Model.Menu>
    {
        public Menu(ISession isession)
            : base(isession)
        {

        }

        public IList<Model.Menu> GetByParent(string webSiteCode, string ParentCode,bool? IsShow)
        {
            List<object> ps = new List<object>();
            string where = "WebSiteCode=?"; ps.Add(webSiteCode);
            if (string.IsNullOrEmpty(ParentCode))
            {
                where += " and ParentCode Is Null";
            }
            else
            {
                where += " and ParentCode=?";
                ps.Add(ParentCode);
            }
            if (IsShow != null)
            {
                where += " and IsShow=?";
                ps.Add(IsShow);
            }
            return Get(where, "Sort desc", ps.ToArray());
        }

        public Common.Result Delete(string webSiteCode, string menuCode)
        {
            var ret = DeleteByWhere("WebSiteCode=? and Code=?"
                , new object[]{webSiteCode,menuCode}
                ,new NHibernate.Type.IType[]{ NHibernateUtil.String,NHibernateUtil.String});
            return ret;
        }

        public override Model.Menu GetModel(object id)
        {
            Model.Menu model = null;
            var list = Get("ID=?", null, id);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public Model.Menu GetModel(string WebSiteCode, string MenuCode)
        {
            Model.Menu model = null;
            var list = Get("WebSiteCode=? and Code=?", null, WebSiteCode, MenuCode);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }
    }
}
